Tidy up use of environment variables in xenbus-hotplug interaction.
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Wed, 7 Dec 2005 11:57:26 +0000 (11:57 +0000)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Wed, 7 Dec 2005 11:57:26 +0000 (11:57 +0000)
I've changed this to only write "backend" (xenbus_backend.root) into
XENBUS_BASE_PATH.  After appending XENBUS_TYPE (also exported) you can
construct the "base path" easily in the scripts.  I've completely
removed writing XENBUS_FRONTEND_ID in favour of reading it from the
store at "$XENBUS_PATH/frontend-id".

Avoid re-reading the store over and over for the frontend domain's vm-path.

The patch also fixes a memory leak in backend_bus_id where we
leak the memory referenced by frontend if the function succeeds.

This work is by Christian Limpach.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c
tools/examples/block

index 4e7bf601b379dee72774a554a9ebf51b52037446..dac16ff4ab555af33b8bd5029476db71d636d97a 100644 (file)
@@ -59,8 +59,6 @@ extern struct semaphore xenwatch_mutex;
 
 #define streq(a, b) (strcmp((a), (b)) == 0)
 
-static char *kasprintf(const char *fmt, ...);
-
 static struct notifier_block *xenstore_chain;
 
 /* If something in array of ids matches this device, return it. */
@@ -209,14 +207,13 @@ static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
                return err;
        if (strlen(frontend) == 0)
                err = -ERANGE;
-
        if (!err && !xenbus_exists(NULL, frontend, ""))
                err = -ENOENT;
 
-       if (err) {
-               kfree(frontend);
+       kfree(frontend);
+
+       if (err)
                return err;
-       }
 
        if (snprintf(bus_id, BUS_ID_SIZE,
                     "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
@@ -224,15 +221,31 @@ static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
        return 0;
 }
 
+static int xenbus_hotplug_backend(struct device *dev, char **envp,
+                                 int num_envp, char *buffer, int buffer_size);
+static int xenbus_probe_backend(const char *type, const char *domid);
+static struct xen_bus_type xenbus_backend = {
+       .root = "backend",
+       .levels = 3,            /* backend/type/<frontend>/<id> */
+       .get_bus_id = backend_bus_id,
+       .probe = xenbus_probe_backend,
+       .bus = {
+               .name  = "xen-backend",
+               .match = xenbus_match,
+               .hotplug = xenbus_hotplug_backend,
+       },
+       .dev = {
+               .bus_id = "xen-backend",
+       },
+};
+
 static int xenbus_hotplug_backend(struct device *dev, char **envp,
                                  int num_envp, char *buffer, int buffer_size)
 {
        struct xenbus_device *xdev;
-       struct xenbus_driver *drv = NULL;
+       struct xenbus_driver *drv;
        int i = 0;
        int length = 0;
-       char *basepath_end;
-       char *frontend_id;
 
        DPRINTK("");
 
@@ -243,9 +256,6 @@ static int xenbus_hotplug_backend(struct device *dev, char **envp,
        if (xdev == NULL)
                return -ENODEV;
 
-       if (dev->driver)
-               drv = to_xenbus_driver(dev->driver);
-
        /* stuff we want to pass to /sbin/hotplug */
        add_hotplug_env_var(envp, num_envp, &i,
                            buffer, buffer_size, &length,
@@ -257,22 +267,7 @@ static int xenbus_hotplug_backend(struct device *dev, char **envp,
 
        add_hotplug_env_var(envp, num_envp, &i,
                            buffer, buffer_size, &length,
-                           "XENBUS_BASE_PATH=%s", xdev->nodename);
-
-       basepath_end = strrchr(envp[i - 1], '/');
-       length -= strlen(basepath_end);
-       *basepath_end = '\0';
-       basepath_end = strrchr(envp[i - 1], '/');
-       length -= strlen(basepath_end);
-       *basepath_end = '\0';
-
-       basepath_end++;
-       frontend_id = kmalloc(strlen(basepath_end) + 1, GFP_KERNEL);
-       strcpy(frontend_id, basepath_end);
-       add_hotplug_env_var(envp, num_envp, &i,
-                           buffer, buffer_size, &length,
-                           "XENBUS_FRONTEND_ID=%s", frontend_id);
-       kfree(frontend_id);
+                           "XENBUS_BASE_PATH=%s", xenbus_backend.root);
 
        /* terminate, set to next free slot, shrink available space */
        envp[i] = NULL;
@@ -281,30 +276,16 @@ static int xenbus_hotplug_backend(struct device *dev, char **envp,
        buffer = &buffer[length];
        buffer_size -= length;
 
-       if (drv && drv->hotplug)
-               return drv->hotplug(xdev, envp, num_envp, buffer,
-                                   buffer_size);
+       if (dev->driver) {
+               drv = to_xenbus_driver(dev->driver);
+               if (drv && drv->hotplug)
+                       return drv->hotplug(xdev, envp, num_envp, buffer,
+                                           buffer_size);
+       }
 
        return 0;
 }
 
-static int xenbus_probe_backend(const char *type, const char *domid);
-static struct xen_bus_type xenbus_backend = {
-       .root = "backend",
-       .levels = 3,            /* backend/type/<frontend>/<id> */
-       .get_bus_id = backend_bus_id,
-       .probe = xenbus_probe_backend,
-       .bus = {
-               .name  = "xen-backend",
-               .match = xenbus_match,
-               .hotplug = xenbus_hotplug_backend,
-       },
-       .dev = {
-               .bus_id = "xen-backend",
-       },
-};
-
-
 static void otherend_changed(struct xenbus_watch *watch,
                             const char **vec, unsigned int len)
 {
index 6868edda45508d3d25f787be13266dca0c8492b0..b6a374c3c4a8f9cb1c897aa79b7509d96d189e8f 100644 (file)
@@ -89,12 +89,12 @@ check_sharing()
     fi
   done
 
-  for dom in $(xenstore-list "$XENBUS_BASE_PATH")
+  local base_path="$XENBUS_BASE_PATH/$XENBUS_TYPE"
+  for dom in $(xenstore-list "$base_path")
   do
-    for dev in $(xenstore-list "$XENBUS_BASE_PATH/$dom")
+    for dev in $(xenstore-list "$base_path/$dom")
     do
-      d=$(xenstore_read_default \
-            "$XENBUS_BASE_PATH/$dom/$dev/physical-device" "")
+      d=$(xenstore_read_default "$base_path/$dom/$dev/physical-device" "")
 
       if [ "$d" == "$devmm" ]
       then
@@ -106,7 +106,7 @@ check_sharing()
             return
           fi
         else
-          local m=$(xenstore_read "$XENBUS_BASE_PATH/$dom/$dev/mode")
+          local m=$(xenstore_read "$base_path/$dom/$dev/mode")
           m=$(canonicalise_mode "$m")
 
           if [ "$m" == 'w' ]
@@ -128,12 +128,10 @@ check_sharing()
 
 same_vm()
 {
-  local thisdom="$XENBUS_FRONTEND_ID"
   local otherdom="$1"
-  local thisvm=$(xenstore-read "/local/domain/$thisdom/vm")
   local othervm=$(xenstore-read "/local/domain/$otherdom/vm")
 
-  [ "$thisvm" == "$othervm" ]
+  [ "$FRONTEND_UUID" == "$othervm" ]
 }
 
 
@@ -240,6 +238,9 @@ case "$command" in
     case $t in 
       phy)
         dev=$(expand_dev $p)
+        FRONTEND_ID=$(xenstore_read "$XENBUS_PATH/frontend-id")
+        FRONTEND_UUID=$(xenstore_read_default \
+            "/local/domain/$FRONTEND_ID/vm" 'unknown')
         claim_lock "block"
         check_device_sharing "$dev" "$mode"
        write_dev "$dev"